/*
 * main.cpp
 *
 *  Created on: Jun 18, 2011
 *      Author: blyth
 */

#include <SimDataReader.h>
#include <PeakReference.h>
#include <iostream>
#include <globals.hh>
#include <TH2D.h>
using namespace std;

int main(int argc, char** argv) {
	if (argc != 4) {
		cout
				<< "Please give simulated data input as the first argument, photons per klong as second, and output file as third"
				<< endl;
		return 0;
	}

	SimDataReader* sdr = new SimDataReader(argv[1], atoi(argv[2]));
	TFile* dFile = new TFile(argv[3], "recreate");
	TH1D* OEVDistrib0 = new TH1D("OEVDistrib0",
			"OEVDistrib 80 MeV Cal Threshold N=1;OEV Energy (MeV);Count",
			10000, 0, 10000);
	TH1D* OEVDistrib1 = new TH1D("OEVDistrib1",
			"OEVDistrib 300 MeV Cal Threshold N=1;OEV Energy (MeV);Count",
			10000, 0, 10000);
	TH1D* OEVDistrib2 = new TH1D("OEVDistrib2",
			"OEVDistrib 80 MeV Cal Threshold N=2;OEV Energy (MeV);Count",
			10000, 0, 10000);
	TH1D* OEVDistrib3 = new TH1D("OEVDistrib3",
			"OEVDistrib 300 MeV Cal Threshold N=2;OEV Energy (MeV);Count",
			10000, 0, 10000);

	double OEVEnergy = 0;
	double energy = 0;
	int hitCount = 0;
	int count = 0;
	while (sdr->ReadNext(OEVEnergy, energy, hitCount) /*&& count < 10000*/) {
		if (energy >= 80 * MeV)
			OEVDistrib0->Fill(OEVEnergy / MeV);
		if (energy >= 300 * MeV)
			OEVDistrib1->Fill(OEVEnergy / MeV);
		if (hitCount == 2) {
			if (energy >= 80 * MeV)
				OEVDistrib2->Fill(OEVEnergy / MeV);
			if (energy >= 300 * MeV)
				OEVDistrib3->Fill(OEVEnergy / MeV);
		}
		count++;
		OEVEnergy = 0;
		energy = 0;
		hitCount = 0;
	}

	delete sdr;
	OEVDistrib0->Write();
	OEVDistrib1->Write();
	OEVDistrib2->Write();
	OEVDistrib3->Write();
	dFile->Close();

	return 0;
}
